/**
 *
 * @author Victor Khimchenko
 */
package impl.game.grid.dnd;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.*;

public abstract class ComponentDND extends JButton {
    protected abstract String getMyClassName();

    protected String shortName;

    protected class MyGestureListener implements DragGestureListener {
        public void dragGestureRecognized(DragGestureEvent dge) {
            Transferable trans = new StringSelection(getMyClassName());
            dge.startDrag(new Cursor(Cursor.HAND_CURSOR), trans);
        }
    }

    public void paint(Graphics g) {
        super.paint(g);    //To change body of overridden methods use File | Settings | File Templates.
        g.drawString(shortName.substring(0, 1), getWidth() - 15, 15);
    }

    protected class MySourceListener implements DragSourceListener {
        public void dragEnter(DragSourceDragEvent dsde) {

        }

        public void dragOver(DragSourceDragEvent dsde) {
        }

        public void dropActionChanged(DragSourceDragEvent dsde) {
        }

        public void dragExit(DragSourceEvent dse) {
        }

        public void dragDropEnd(DragSourceDropEvent dsde) {
        }
    }

    private DragSource dragSource;
    private DragGestureListener dgListener;
    private DragSourceListener dsListener;

    public ComponentDND(String name, String shortName) {
        super(name);
        this.shortName = shortName;
        // SOURCE
        dragSource = DragSource.getDefaultDragSource();
        dgListener = new MyGestureListener();
        dsListener = new MySourceListener();
        dragSource.createDefaultDragGestureRecognizer(
                this, DnDConstants.ACTION_COPY_OR_MOVE, this.dgListener);
        dragSource.addDragSourceListener(dsListener);

    }
}

